package com.vitabuybuy.tv.widget.selector

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import com.blankj.utilcode.util.ScreenUtils
import com.library.common.ext.setOnclickNoRepeat
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.RegionDto
import kotlin.math.roundToInt

class BottomDialog(context: Context, deep: Int) : Dialog(context, R.style.bottom_dialog) {
    private var selector: Selector = Selector(context, deep)

    init {
        setContentView(selector.view)
        setCanceledOnTouchOutside(false)
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = (ScreenUtils.getScreenHeight() * 1.0 / 2).roundToInt()
        window?.attributes = params
        window?.setGravity(Gravity.BOTTOM)

        setOnclickNoRepeat(selector.tvCancel,selector.tvConfirm){v->
            when (v.id) {
                R.id.tv_confirm -> {
                    selector.callbackInternal()
                    dismiss()
                }
                R.id.tv_cancel -> {
                    dismiss()
                }
            }
        }
    }

    fun setOnAddressSelectedListener(listener: SelectedListener?) {
        selector.setSelectedListener(listener)
    }

    fun setDataRegion(regionlist: MutableList<RegionDto>) {
        selector.setDataProvider { receiver ->
            receiver.send(regionlist)
        }
    }

}